package com.ptszyxx.popose.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import com.ptszyxx.popose.common.event.WeChatEvent;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.ptszyxx.popose.common.constants.AppConstant;
import com.ysg.bus.RxBus;

public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
    private IWXAPI api;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //通过WXAPIFactory工厂获取IWXApI的示例
        api = WXAPIFactory.createWXAPI(this, AppConstant.APPID_WECHAT, true);
        //将应用的appid注册到微信
        api.registerApp(AppConstant.APPID_WECHAT);
        //注意：
        //第三方开发者如果使用透明界面来实现WXEntryActivity，需要判断handleIntent的返回值，如果返回值为false，则说明入参不合法未被SDK处理，应finish当前透明界面，避免外部通过传递非法参数的Intent导致停留在透明界面，引起用户的疑惑
        try {
            boolean result = api.handleIntent(getIntent(), this);
            if (!result) {
                Log.i("===微信===", "onCreate: 参数不合法");
                finish();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        api.handleIntent(data, this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);
        finish();
    }

    @Override
    public void onReq(BaseReq baseReq) {
    }

    @Override
    public void onResp(BaseResp baseResp) {
        String code = ((SendAuth.Resp) baseResp).code;

        switch (baseResp.errCode) {
            case BaseResp.ErrCode.ERR_OK:
                Log.i("lnk", "onResp: " + baseResp.errStr);
                Log.i("lnk", "onResp: " + baseResp.errCode);
                Log.i("lnk", "onResp: " + ((SendAuth.Resp) baseResp).state);
                Log.i("lnk", "onResp:" + ((SendAuth.Resp) baseResp).openId);
                Log.i("lnk", "onResp-code:" + ((SendAuth.Resp) baseResp).code);
                RxBus.getDefault().post(new WeChatEvent(true, code, "授权成功"));
                finish();
                break;
            case BaseResp.ErrCode.ERR_USER_CANCEL:
                RxBus.getDefault().post(new WeChatEvent(false, code, "用户取消"));
                finish();
                break;
            case BaseResp.ErrCode.ERR_AUTH_DENIED:
                RxBus.getDefault().post(new WeChatEvent(true, code, "用户拒绝"));
                finish();
                break;
            default:
                RxBus.getDefault().post(new WeChatEvent(false, code, "用户返回"));
                finish();
                break;
        }

    }

}
